package com.yyjj.hangqing.fragment;

import android.graphics.Color;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.ViewGroup;
import android.widget.GridLayout;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.vise.xsnow.http.ViseHttp;
import com.vise.xsnow.http.callback.ACallback;
import com.yyjj.hangqing.R;
import com.yyjj.hangqing.adapter.CoinListAdapter;
import com.yyjj.hangqing.entity.BaseTResp;
import com.yyjj.hangqing.entity.CoinExchangeEntity;

import java.util.ArrayList;

import tech.com.commoncore.app.FastManager;
import tech.com.commoncore.base.BaseRefreshLoadFragment;
import tech.com.commoncore.constant.ApiConstant;
import tech.com.commoncore.utils.DataUtils;
import tech.com.commoncore.view.SpaceItemDecoration;

/**
 *
 */

public class CoinListFragment extends BaseRefreshLoadFragment{
    public String title;
    TextView content_tv;
    public static CoinListFragment newInstance(String title) {
        Bundle args = new Bundle();
        args.putString("title",title);
        CoinListFragment fragment = new CoinListFragment();
        fragment.setArguments(args);
        return fragment;
    }

    @Override
    public int getContentLayout() {
        return R.layout.hq_refresh_notitle;
    }

    @Override
    public void initView(Bundle savedInstanceState) {
        title = getArguments().getString("title");
        mRecyclerView.addItemDecoration(new SpaceItemDecoration(1));

    }

    @Override
    public BaseQuickAdapter getAdapter() {
        adapter =  new CoinListAdapter(R.layout.hq_item_coin);
        return adapter;
    }
    CoinListAdapter adapter;
    TextView textViewHead;
    @Override
    public void loadData(int page) {
        getData(page);
    }
    void getData(int page){
        if(page>1){
            FastManager.getInstance().getHttpRequestControl().httpRequestSuccess(getIHttpRequestControl(),null,null);
            return;
        }

//         https://d.wanjinig.cn/yapi/Currency/golden_exchange_currency?size=20&exchange=bitmex&sort=price_total&order=desc
        ViseHttp.GET("/yapi/Currency/golden_exchange_currency")
                .baseUrl(ApiConstant.BASE_URL)
                .addParam("size","40")
                .addParam("exchange",title)
                .request(new ACallback<BaseTResp<CoinExchangeEntity>>() {
                    @Override
                    public void onSuccess(BaseTResp<CoinExchangeEntity> data) {
                        if(data.code ==1){
                            FastManager.getInstance().getHttpRequestControl().httpRequestSuccess(getIHttpRequestControl(),data.data.getData(),null);
                            if(adapter!=null && data.data.getInfo()!=null && !DataUtils.isEmpty(data.data.getInfo().getInfo())&&textViewHead==null){
                                textViewHead = new TextView(mContext);
                                ViewGroup.LayoutParams layoutParams = new ViewGroup.LayoutParams(-1,-2);
                                textViewHead.setLayoutParams(layoutParams);
                                textViewHead.setTextColor(getResources().getColor(R.color.text_title));
                                textViewHead.setTextSize(14);
                                textViewHead.setMaxLines(6);
                                textViewHead.setPadding(10,10,10,10);
                                textViewHead.setText(data.data.getInfo().getInfo());
                                textViewHead.setEllipsize(TextUtils.TruncateAt.END);
                                adapter.addHeaderView(textViewHead);
                            }
                        }else{
                            FastManager.getInstance().getHttpRequestControl().httpRequestSuccess(getIHttpRequestControl(),null,null);
                        }
                    }

                    @Override
                    public void onFail(int errCode, String errMsg) {
                        FastManager.getInstance().getHttpRequestControl().httpRequestSuccess(getIHttpRequestControl(),null,null);
                    }
                });
    }

//    @Override
//    protected void onLoadErrorClick() {
//        getData(1);
//    }

}
